package com.stylefeng.guns.modular.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitRouteID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.RouteMapper;
import com.stylefeng.guns.modular.system.model.Consignee;
import com.stylefeng.guns.modular.system.model.Consigner;
import com.stylefeng.guns.modular.system.model.Route;
import com.stylefeng.guns.modular.system.service.IRouteService;

/**
 * 物流线路控制器
 *
 * @author fengshuonan
 * @Date 2018-04-03 17:06:48
 */
@Controller
@RequestMapping("/route")
public class RouteController extends BaseController {

    private String PREFIX = "/system/route/";

    @Autowired
    private IRouteService routeService;
    @Autowired
    private RouteMapper routeMapper;

    /**
     * 跳转到物流线路首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "route.html";
    }

    /**
     * 跳转到添加物流线路
     */
    @RequestMapping("/route_add")
    public String routeAdd() {
        return PREFIX + "route_add.html";
    }

    /**
     * 跳转到修改物流线路
     */
    @RequestMapping("/route_update/{routeId}")
    public String routeUpdate(@PathVariable Integer routeId, Model model) {
        Route route = routeService.selectById(routeId);
        model.addAttribute("item",route);
        LogObjectHolder.me().set(route);
        return PREFIX + "route_edit.html";
    }

    /**
     * 获取物流线路列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Route> page = new PageFactory<Route>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(routeMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Route> entityWrapper = new EntityWrapper<>();
    		Wrapper<Route> wrapper = entityWrapper.like("route_start", condition);
    		page.setRecords(routeMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增物流线路
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Route route) {
        routeService.insert(route);
        return SUCCESS_TIP;
    }
    
    
    /**
     * 通过物流路线编号，模糊查询所有线路信息
     */
    @RequestMapping(value="/listbyrouteid")
    @ResponseBody
    public Object listByName(@RequestParam String routeId){
    	EntityWrapper<Route> entityWrapper = new EntityWrapper<>();
    	Wrapper<Route> wrapper = entityWrapper.like("route_id", routeId);
    	return routeService.selectList(wrapper);
    }

    /**
     * 删除物流线路
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer routeId) {
        routeService.deleteById(routeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改物流线路
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Route route) {
        routeService.updateById(route);
        return SUCCESS_TIP;
    }

    /**
     * 物流线路详情
     */
    @RequestMapping(value = "/detail/{routeId}")
    @ResponseBody
    public Object detail(@PathVariable("routeId") Integer routeId) {
        return routeService.selectById(routeId);
    }
}
